package main

import "fmt"

/*
https://golang-minibear2333.github.io/2.func-containers/2-4-map/
*/
func main() {

	var m map[string]string
	m = make(map[string]string)
	m["name"] = "cm"
	m["sex"] = "man"
	m["del"] = "1"
	// 遍历
	for key := range m {
		fmt.Println("key:" + key + ",value:" + m[key])
	}
	// 删除 key
	delete(m, "del")

	key := "name"
	if value, ok := m[key]; ok {
		fmt.Println(key, "存在，值为：", value)
	} else {
		fmt.Println(key, "不存在")
	}

	// map 不能深拷贝
	originalMap := make(map[string]int)
	originalMap["one"] = 1
	originalMap["two"] = 2
	// 创建目标 map
	targetMap := make(map[string]int)
	for key, value := range originalMap {
		targetMap[key] = value
		fmt.Println("key:"+key+" ;value:", value)
	}

	// map 指针，考虑深拷贝
	originalMap2 := make(map[string]*int)
	var num = 1
	originalMap2["one"] = &num
	//
	targetMap2 := make(map[string]*int)
	for key, value := range originalMap2 {
		var tmpNum = *value
		targetMap2[key] = &tmpNum
		fmt.Println("带指针，map 深拷贝，key:"+key+" ;value:", value)
	}

}
